package com.hmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmall.config.JwtProperties;
import com.hmall.domain.dto.LoginFormDTO;
import com.hmall.domain.dto.RegisterDTO;
import com.hmall.domain.po.Datas;
import com.hmall.domain.po.Information;
import com.hmall.domain.po.User;
import com.hmall.domain.vo.UserLoginVO;
import com.hmall.mapper.DataMapper;
import com.hmall.mapper.InformationMapper;
import com.hmall.mapper.UserMapper;
import com.hmall.service.IInformationService;
import com.hmall.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.utils.JwtTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.el.LambdaExpression;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HBQ
 * @since 2025-10-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private DataMapper dataMapper;

    @Autowired
    private JwtTool jwtTool;
    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DataServiceImpl dataServiceImpl;
    @Autowired
    private IInformationService informationService;

    /**
     * 登录
     *
     * @param loginFormDTO
     * @return
     */
    @Override
    public UserLoginVO login(LoginFormDTO loginFormDTO) {

        // 1.判断账号密码是否为空
        if (loginFormDTO.getUsername() == null || loginFormDTO.getPassword() == null){
            throw new RuntimeException("账号密码不能为空");
        }

        // 2.校验账号密码是否正确
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, loginFormDTO.getUsername())
                .eq(User::getPassword, loginFormDTO.getPassword());
        User user = getOne(queryWrapper);
        if (user == null){
            throw new RuntimeException("账号密码错误");
        }

        // 3.生成token
        String token = jwtTool.createToken(user.getId(), jwtProperties.getTokenTTL());

        // 4.保存数据，登录次数+1
        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setToken(token);
        userLoginVO.setUserId(user.getId());
        userLoginVO.setUsername(user.getUsername());
        userLoginVO.setCount(user.getCount()+1);
        userMapper.saveUserCount(userLoginVO);

        return userLoginVO;
    }

    /**
     * 注册
     * @return
     */
    @Override
    public Boolean register(RegisterDTO registerDTO) {

        // 1.判断账号/密码/手机号是否为空

        if (registerDTO.getUsername() == null || registerDTO.getPassword() == null || registerDTO.getPhone() == null){
            throw new RuntimeException("账号/密码/手机号为空");
        }

        // 2.判断账号是否已存在
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, registerDTO.getUsername());
        User user = getOne(queryWrapper);
        if (user != null){
            throw new RuntimeException("账号已存在");
        }

        // 3.判断手机号是否已存在
        User userPhone = getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, registerDTO.getPhone()));
        if (userPhone != null){
            throw new RuntimeException("手机号已存在");
        }
        // 注册成功，登录次数设为0
        User users = new User();
        users.setUsername(registerDTO.getUsername());
        users.setPassword(registerDTO.getPassword());
        users.setPhone(registerDTO.getPhone());
        users.setCount(0);
        save(users);  // 保存到用户表中
        LambdaQueryWrapper<User> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(User::getUsername, users.getUsername());
        User one = getOne(queryWrapper1);
        Information information = new Information();
        information.setUserId(one.getId());
        Datas data = new Datas();
        data.setUserId(one.getId());
        // 向data表插入数据，user_id为当前用户id
        dataServiceImpl.save(data);
        dataMapper.saveUser(data,one.getId());
        informationService.save(information);
        informationMapper.saveUser(information,one.getId());

        return true;
    }
}
